<template>
  <div>
    <el-dialog :title="curid ? '修改' : '添加'" :visible.sync="obj.isshow">
      <el-form :model="form">
        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格属性" :label-width="formLabelWidth">
          <div class="box">
            <el-input v-model="form.name" placeholder="请输入内容"></el-input>
            <el-button type="primary" plain @click="add"
              >新增规格属性</el-button
            >
          </div>
        </el-form-item>

        <el-form-item
          :label-width="formLabelWidth"
          v-for="(item, index) in attrs"
          :key="index"
        >
          <div class="box" v-if="attrs != []">
            <el-input v-model="item.name" placeholder="请输入内容"></el-input>
            <el-button type="danger" plain @click="delSpecs(index)"
              >删除</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <!-- 
            active-value: switch启用时的值
            inactive-value: switch禁用时的值
          -->
          <el-switch
            :active-value="1"
            :inactive-value="2"
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="canle">取 消</el-button>
        <el-button type="primary" v-if="curid == ''" @click="addSpecs"
          >添加</el-button
        >
        <el-button type="primary" v-else @click="updateSpecs">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { specsadd, specsinfo, specsedit } from "../../request/api.js";
import { successMsg, failedMsg } from "../../tools/tools.js";

export default {
  props: ["obj"],
  data() {
    return {
      curid: "",
      attrs: [],
      form: {
        specsname: "",
        status: 1,
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    updateSpecs() {
      let arr = []; //存放所有规格属性值
      this.attrs.forEach((items) => {
        //通过遍历将数组对象中的值遍历出来
        arr.push(items.name);
      });
      this.form.attrs = arr.join();
      this.form.id = this.curid;
      console.log(this.form);
      specsedit(this.form).then((d) => {
        if (d.data.code == 200) {
          successMsg(d.data.msg);
          this.$emit("addSpeac");
          this.empty();
        }else{
            failedMsg(d.data.msg)
          }
      });
    },
    getInfo(id) {
      this.curid = id;
      specsinfo({ id })
        .then((d) => {
          if (d.data.code == 200) {
            this.form = d.data.list[0];
            d.data.list[0].attrs.forEach((d) => {
              let obj = { name: d };
              this.attrs.push(obj);
            });
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    canle() {
      //取消按钮
      this.$emit("canle");
      this.empty();
      this.curid = "";
    },
    add() {
      //添加规格属性，往内容插入到数组中，显示在下方
      let obj = { name: this.form.name };
      this.attrs.push(obj); //插入到数组
      this.form.name = ""; //清空输入框
    },
    delSpecs(n) {
      //删除规格属性
      this.attrs.splice(n, 1); //通过下标值
    },
    addSpecs() {
      let arr = []; //存放所有规格属性值
      this.attrs.forEach((items) => {
        //通过遍历将数组对象中的值遍历出来
        arr.push(items.name);
      });
      this.form.attrs = arr.join();
      specsadd(this.form)
        .then((d) => {
          //调用规格添加API
          if (d.data.code == 200) {
            successMsg(d.data.msg);
            this.$emit("addspecsadd");
            this.empty();
          }else{
            failedMsg(d.data.msg)
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    empty() {
      (this.form = {
        specsname: "",
        status: 1,
      }),
        (this.attrs = []);
        this.curid =''
    },
  },
};
</script>

<style scoped>
.box {
  display: flex;
}
</style>